package com.invengo.module_check.data.repsitory.offlinedata.source.impl;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.invengo.base.http.RetrofitClient;
import com.invengo.base.utils.RequestBodyUtils;
import com.invengo.library_db.GreenDaoManager;
import com.invengo.library_db.entity.module_check.offline.BookJson;
import com.invengo.library_db.entity.module_check.ColumBean;
import com.invengo.library_db.entity.module_check.offline.FlowBean;
import com.invengo.library_db.entity.module_check.offline.FlowType;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.library_db.entity.module_check.ShelfJson;
import com.invengo.library_db.entity.module_check.StructBean;
import com.invengo.library_db.entity.module_check.offline.UserBean;
import com.invengo.module_check.data.api.BusinessApi;
import com.invengo.module_check.data.api.OfflineApi;
import com.invengo.module_check.data.repsitory.offlinedata.source.NetModelSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import me.goldze.mvvmhabit.http.BaseBean;
import me.goldze.mvvmhabit.http.UpdateResult;
import me.goldze.mvvmhabit.utils.KLog;

public class HttpNetModelSourceImpl implements NetModelSource {
    private OfflineApi service;
    private BusinessApi businessApi;


    public HttpNetModelSourceImpl() {
        businessApi = RetrofitClient.getInstance().create(BusinessApi.class);
        service = RetrofitClient.getInstance().create(OfflineApi.class);
    }

    /**
     * 获取所有用户
     *
     * @return {@link Observable}<{@link BaseBean}<{@link List}<{@link UserBean}>>>
     */
    @Override
    public Observable<BaseBean<List<UserBean>>> getAllUser() {
        return service.getAllUser();
    }

    /**
     * get结构
     *
     */
    @Override
    public Observable<BaseBean<List<StructBean>>> getStruct() {
        return service.getStruct();
    }

    /**
     * 获取所有图层
     *
     * @return {@link Observable}<{@link BaseBean}<{@link ShelfJson}>>
     */
    @Override
    public Observable<BaseBean<ShelfJson>> getAllShelf() {
        return service.getAllShelfJson();
    }

    @Override
    public Observable<BaseBean<Long>> getBookCount(String beginTime, String endTime) {
        HashMap<String, Object> params = new HashMap<>();
        if (!TextUtils.isEmpty(beginTime)) {
            params.put("beginTime", beginTime + " 00:00:00");
        }
        if (!TextUtils.isEmpty(endTime)) {
            params.put("endTime", endTime);
        }
        return service.getBookCount(RequestBodyUtils.parse(params));
        /**
         * 我们自己用的是body，山东大学用的是query
         */
//        return service.getBookCount(beginTime,endTime);
    }


    @Override
    public Observable<BaseBean<BookJson>> getPagingDataOfBook(int pageIndex, int pageSize, String beginTime, String endTime) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        if (!TextUtils.isEmpty(beginTime)) {
            params.put("beginTime", beginTime + " 00:00:00");
        }
        if (!TextUtils.isEmpty(endTime)) {
            params.put("endTime", endTime);
        }
        return service.getPagingDataOfBook(RequestBodyUtils.parseWithAnd(params));
    }

    @Override
    public Observable<BaseBean<BookJson>> getPagingDataOfBook(String pageIndex, String pageSize, String beginTime, String endTime) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        if (!TextUtils.isEmpty(beginTime)) {
            params.put("beginTime", beginTime);
        }
        if (!TextUtils.isEmpty(endTime)) {
            params.put("endTime", endTime);
        }
        return service.getPagingDataOfBook(RequestBodyUtils.parseWithAnd(params));
    }

    @Override
    public List<FlowBean> getBusinessData() {
        return GreenDaoManager.getInstance().getbusinessData();
    }

    @Override
    public Observable<BaseBean<UserBean>> checkLogin(String account, String password) {
//        KLog.e("查询网络");
        Map<String, Object> params = new HashMap();
        params.put("account", account);
        params.put("password", password);
        return businessApi.checkLogin(RequestBodyUtils.parse(params));
    }
    public Observable<BaseBean<UpdateResult>> uploadData(FlowBean bean) {
        KLog.e("执行顺序===> [ " + bean.getFlowType() + " " + bean.getDateTime());
        switch (bean.getFlowType()) {
            case FlowType.FLOW_DOWN:
                return businessApi.updateBookInfo(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_LISTING:
                return businessApi.updateBookInfo(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_LISTING_GUIDE:
                return businessApi.listingGuide(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_INVENTORY:
                return businessApi.inventoryBook(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_PARALLEL:
                return businessApi.parallelLogs(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_REMOVE:
                return businessApi.removeOld(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_CHANGE:
                return businessApi.updateBookInfo(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_CHANGE_LOG:
                return businessApi.changeShelfLog(RequestBodyUtils.assembleOffline(bean.getBodyJson(), true));
            case FlowType.FLOW_FORCE_REVOKE:
                return businessApi.forceRevokeBook(RequestBodyUtils.assembleOffline(bean.getBodyJson(), false));
            case FlowType.FLOW_FORCE:
                return businessApi.forcePositioning(RequestBodyUtils.assembleOffline(bean.getBodyJson(), false));
            case FlowType.FLOW_FIRST:
                return businessApi.updateFirstBook(RequestBodyUtils.assembleOffline(bean.getBodyJson(), false));
            case FlowType.FLOW_FIRST_TASK:
                List<String> data = new Gson().fromJson(bean.getBodyJson(), new TypeToken<List<String>>() {
                }.getType());
                return businessApi.createupdatetask(data.get(0), data.get(1), data.get(2));
        }
        return null;
    }

    @Override
    public void deleteBusinessBean(FlowBean bean) {
        GreenDaoManager.getInstance().deleteBusinessBean(bean);
    }

    @Override
    public void saveStruct2DB(List<StructBean> collect) {
        GreenDaoManager.getInstance().saveStruct2DB(collect);
    }

    @Override
    public List<StructBean> getStructData() {
        return GreenDaoManager.getInstance().getAllStruct();
    }

    @Override
    public void removeAllColum() {
        GreenDaoManager.getInstance().removeAllColum();
    }

    @Override
    public Long getAllColumCount() {
        return GreenDaoManager.getInstance().getAllColum();
    }

    @Override
    public List<ColumBean> getColumData(StructBean bean) {
        return GreenDaoManager.getInstance().getColumData(bean);
    }

    @Override
    public StructBean getStructByShelf(ShelfBean bean) {
        return GreenDaoManager.getInstance().getStructByShelf(bean);
    }
}
